package com.secsbrain.development.web.controller;

import java.util.List;

import com.secsbrain.cmdb.client.iface.HostInfoService;
import com.secsbrain.common.ErrorCodeEnum;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.development.web.dto.ServerDetailDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.secsbrain.common.ResponseVo;
import com.secsbrain.cmdb.client.model.App;
import com.secsbrain.cmdb.client.model.HostDetail;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 服务器
 */
@RestController
@RequestMapping("api/server")
@ApiIgnore
public class ServerController {
	
    private static final Logger logger = LoggerFactory.getLogger(ServerController.class);

    @Autowired
    private HostInfoService hostInfoService;

    /**
     * 服务器详情
     */
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public ResponseResult serverDetail(@PathVariable("id") long id) {
    	ResponseVo<HostDetail> hostDetailVo=hostInfoService.queryHostDetailById(id);
    	if(hostDetailVo.isSuccess()){
    		HostDetail hostDetail=hostDetailVo.getData();
    		ServerDetailDTO serverDetailDTO = new ServerDetailDTO();
    		serverDetailDTO.setCpu(hostDetail.getCpu()+"");
    		serverDetailDTO.setHost(hostDetail.getHost());
    		serverDetailDTO.setHostName(hostDetail.getHostName());
    		serverDetailDTO.setId(hostDetail.getId());
    		serverDetailDTO.setMemory(hostDetail.getMemory()+"");
    		serverDetailDTO.setOs(hostDetail.getOs());
    		serverDetailDTO.setServerDesc(hostDetail.getServerDesc());
    		List<App> appList=hostDetail.getAppList();
    		StringBuffer stringBuffer=new StringBuffer();
    		for(App app:appList){
    			stringBuffer.append(app.getName()+",");
    		}
    		stringBuffer.deleteCharAt(stringBuffer.length()-1);
    		serverDetailDTO.setAppName(stringBuffer.toString());
    		serverDetailDTO.setServerIp(hostDetail.getServerIp());
    		serverDetailDTO.setUpdatedTime(hostDetail.getUpdateTime());
    		serverDetailDTO.setEnv(hostDetail.getEnvName());
    		return new ResponseResult<>(serverDetailDTO);
    	}else{
    		logger.error("查询主机失败："+hostDetailVo.getMsg());
			return new ResponseResult<>(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),"查询主机失败");

		}
    	
    }

}
